/**
 * @(#)LoadActivity.java 1.0 2013/9/17
 * Copyright 2013 PD, Inc. All rights reserved.
 */
package cn.broadin.app.hotel.startWebapp;


import cn.broadin.app.hotel.R;
import cn.broadin.app.hotel.factory.ApplicationFactory;
import cn.broadin.app.hotel.task.AuthTask;
import cn.broadin.app.hotel.task.DownLoadTask;
import cn.broadin.app.hotel.task.VersionTask;
import cn.broadin.app.hotel.util.PDUtils;
import cn.broadin.app.hotel.version.VersionInfo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * LoadActivity 加载Activity
 * @author fenghui
 *
 */
public class LoadActivity extends Activity{
	private static final String TAG = "LoadActivity";					//log tag
	private LinearLayout errorPanel = null;								//错误消息面板
	private TextView txtErrorMsg = null;								//错误消息内容
	private AnimationDrawable loadAnimation = null;						//加载动画
	private boolean showErrorFlag = false;								//显示错误消息
	private ImageView imgLoadView = null;								//加载动画视图
	private VersionTask versionTask = null;								//版本task
	private AuthTask authTask = null;									//鉴权task
	private DownLoadTask downTask = null;								//下载task
	private Context context = null;										//context对象
	private PDUtils pdUtils = PDUtils.getInstance();		//工具类对象
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		context = this;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.load_page);								//加载load视图
		initView();														//初始化操作
	}
	
	/**
	 * 初始化视图
	 */
	private void initView() {
		errorPanel = (LinearLayout)findViewById(R.id.errorPanel);		//错误消息控件
		txtErrorMsg = (TextView)findViewById(R.id.errorText);			//错误消息内容控件
		imgLoadView = (ImageView) findViewById(R.id.load_imgView);		//加载滚动view
		loadAnimation = (AnimationDrawable) imgLoadView.getBackground();//加载动画对象
		imgLoadView.post(new Runnable() {
			@Override
			public void run() {
				loadAnimation.start();
			}
		});
		Log.i(TAG, "begin get version data.");
		versionTask = new VersionTask(this);							//获取版本信息
		versionTask.execute();
	}
	
	/**
	 * 处理版本信息
	 * @param version
	 */
	public void postVersionData(VersionInfo version) {
		if(version != null) {
			//当前安装程序版本号
			int verCode = pdUtils.getVerCode(context, ApplicationFactory.getApplication().APP_PACKAGENAME());
			Log.i(TAG, "currverCode: " + verCode);
			//服务器获取到的程序版本号
			int serverVerCode = Integer.parseInt(version.getVerCode());
			Log.i(TAG, "serverVerCode: " + serverVerCode);
			if(serverVerCode > verCode) {
				loadAnimation.stop();				//停止动画
				//下载新版本apk文件
				downTask = new DownLoadTask(this);
				String[] arrArgs = new String[]{ApplicationFactory.getApplication().VERSIONURL() + version.getApkname(), ApplicationFactory.getApplication().APKFILENAME()};
				downTask.execute(arrArgs);
				return;
			}
		}
		//需要鉴权处理
		if(ApplicationFactory.getApplication().IS_NEED_CHECKUSER()) {
			//进行鉴权处理
			Log.i(TAG, "begin get auth data.");
			authTask = new AuthTask(this);
			authTask.execute();
		} else {
			toMainPage();
		}
	}
	
	/**
	 * 跳转到主页面
	 */
	public void toMainPage() {
		Intent intent = new Intent(this, StartWebView.class);
		startActivity(intent);
		finish();
	}
	
	/**
	 * 显示错误消息提示
	 * @param msg 提示内容
	 */
	public void showEeeorMsg(String msg) {
		showErrorFlag = true;
		loadAnimation.stop();
		txtErrorMsg.setText(msg);
		errorPanel.setVisibility(View.VISIBLE);
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		finish();
	}
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if(showErrorFlag && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
			finish();
			return false;
		} else {
			return super.dispatchKeyEvent(event);
		}
	}
}